package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.constant.MessageConstant;
import org.dey.entity.Result;
import org.dey.mapper.MemberMapper;
import org.dey.pojo.Member;
import org.dey.pojo.Order;
import org.dey.pojo.PageVo;
import org.dey.pojo.User;
import org.dey.service.MemberService;
import org.dey.service.NumbersService;
import org.dey.service.OrderService;
import org.dey.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author deyran
 * @classname
 * @see ServiceImpl
 */
@Service
@Transactional
public class MemberServiceImpl extends ServiceImpl<MemberMapper,Member> implements MemberService {

    @Autowired
    MemberMapper memberMapper;
    @Autowired
    NumbersService numbersService;
    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;

    @Override
    public PageVo<Member> querymember(Member member, int pageNum, int pagesize) {

        //组装条件
        QueryWrapper<Member> queryWrapper = new QueryWrapper<Member>();
        //根据档案号模糊查询//根据手机号模糊查询//根据名字模糊查询
        if(StringUtils.isNotBlank(member.getName())) {
            queryWrapper.or().like("name", member.getName());
        }
        if(StringUtils.isNotBlank(member.getFileNumber())) {
            queryWrapper.or().like("file_number", member.getFileNumber());
        }
        if(StringUtils.isNotBlank(member.getPhoneNumber())) {
            queryWrapper.or().like("phone_number", member.getPhoneNumber());
        }

        PageHelper.startPage(pageNum, pagesize);
        List<Member> memberinfos = this.list(queryWrapper);

        for (Member memberinfo : memberinfos) {
            Integer healthmanager = memberinfo.getHealthmanager();
            Integer uid = memberMapper.queryUserRole(healthmanager);
            User user = userService.getById(uid);
            memberinfo.setHealthmanagername(user.getUsername());
        }
        PageInfo<Member> pageInfo = new PageInfo<Member>(memberinfos);

        PageVo<Member> pageVo = new PageVo<Member>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }


    @Override
    public Result insertmember(Member member) {
        Result result =null;
        member.setFileNumber(numbersService.memberid());
        Date time = new Date();
        member.setRegTime(time);

        int uid = memberMapper.queryUserRole(member.getHealthmanager());
        User user = userService.queryuserbyid(uid);
        member.setHealthmanagername(user.getUsername());

        boolean one = this.save(member);
        if(one){
            result = new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.ADD_MEMBER_FAIL);
        }
        //前端弹框取值==> res.data.message

        return result;
    }

    @Override
    public Result delmember(int id) {
        Result result =null;
        boolean b = this.removeById(id);
        if(b){
            result = new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return result;
    }

    @Override
    public Member querymemberbyid(int id) {
        return this.getById(id);
    }

    @Override
    public Result updatememberbyid(Member member) {
        Result result =null;
        boolean b = this.updateById(member);
        if(b){
            result = new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.EDIT_MEMBER_FAIL);
        }
        return result;
    }

    @Override
    public Member getByName(String name) {
        //组装条件
        QueryWrapper<Member> queryWrapper = new QueryWrapper<Member>();
        queryWrapper.eq("name",name);
        Member one = this.getOne(queryWrapper);
        return one;
    }

    @Override
    public List<User> getUserRole(){
        List<User> users = new ArrayList<User>();
        List<Integer> uids = memberMapper.queryallUserRole(2);
        for (Integer uid : uids) {
            User user = userService.getById(uid);
            users.add(user);
        }
        return users;
    }

    @Override
    public Member queryRaByRaId(int id) {

        Order byId = orderService.getById(id);

        Integer memberId = byId.getMemberId();

        Member byId1 = this.getById(memberId);

        return byId1;

    }

}
